package chj.Bookmanage;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import chj.Database.CDataBase;
import chj.bookface.BKJPanel;
import chj.BookInformation.Bookop;
//import connection;
import chj.Database.StringUtil;
public class Bupdate {
    //书籍修改
    public static void bookUpDataFram() {

        JFrame frame = new JFrame("书籍修改");
        frame.setLayout(null);

        BKJPanel pan=new BKJPanel("src/chj/image/bg.jpg"); //背景图地址
        pan.setLayout(null);
        //开8个面板，方便设置位置
        JPanel pan1 = new JPanel();//
        JPanel pan2 = new JPanel();//
        JPanel pan3 = new JPanel();//
        JPanel pan4 = new JPanel();//
        JPanel pan5 = new JPanel();//
        JPanel pan6 = new JPanel();
        JPanel pan7 = new JPanel();
        JPanel pan8 = new JPanel();
         //文本框
        JTextField textBno = new JTextField();//
        JTextField textBname = new JTextField();//
        JTextField textBauthor = new JTextField();//
        JTextField textBpress = new JTextField();//
        JTextField textBCategory = new JTextField();//
        JTextField textBnumber = new JTextField();//
        //提示框
        JLabel label1 = new JLabel("修 改 界 面");
        JLabel label2 = new JLabel("书 籍 编 号  ");
        JLabel label3 = new JLabel("书 籍 名 称  ");
        JLabel label4 = new JLabel("书 籍 作 者  ");
        JLabel label5 = new JLabel("出 版 商     ");
        JLabel label6 = new JLabel("书 籍 类 别  ");
        JLabel label7 = new JLabel("书 籍 数 量  ");

        //按钮
        JButton button1 = new JButton("提交");

        // 设置字体
        Font font = new Font("宋体", Font.BOLD, 50); // 标题字体
        Font f = new Font("宋体", Font.BOLD, 25); // 标签和文本框字体
        // 设置按钮的样式
        button1.setBackground(new Color(30, 144, 255)); // 设置背景色（使用RGB值）
        button1.setForeground(Color.WHITE); // 设置前景色（字体颜色）
        button1.setBorder(BorderFactory.createEtchedBorder()); // 设置边框样式
        button1.setFocusPainted(false); // 去掉按钮获得焦点时的边框
        button1.setFont(f); // 设置按钮字体
        button1.setCursor(new Cursor(Cursor.HAND_CURSOR)); // 当鼠标移动到按钮上时改变光标

        // 添加鼠标悬停效果
        button1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                button1.setBackground(new Color(70, 130, 180)); // 悬停时改变背景色
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                button1.setBackground(new Color(30, 144, 255)); // 鼠标离开时恢复背景色
            }
        });
        // 设置文本框的大小
        Dimension textSize = new Dimension(200, 30);
        textBno.setPreferredSize(textSize);
        textBname.setPreferredSize(textSize);
        textBauthor.setPreferredSize(textSize);
        textBpress.setPreferredSize(textSize);
        textBCategory.setPreferredSize(textSize);
        textBnumber.setPreferredSize(textSize);

        button1.setPreferredSize(new Dimension(100, 40));

        // 设置字体样式
        label1.setFont(font);
        label2.setFont(f);
        textBno.setFont(f);
        label3.setFont(f);
        textBname.setFont(f);
        label4.setFont(f);
        textBauthor.setFont(f);
        label5.setFont(f);
        textBpress.setFont(f);
        label6.setFont(f);
        textBCategory.setFont(f);
        label7.setFont(f);
        textBnumber.setFont(f);

        button1.setFont(f);

        // 设置面板背景色和边框
        pan1.setOpaque(false);
        pan2.setOpaque(false);
        pan3.setOpaque(false);
        pan4.setOpaque(false);
        pan5.setOpaque(false);
        pan6.setOpaque(false);
        pan7.setOpaque(false);
        pan8.setOpaque(false);

        // 设置面板的位置
        pan1.setBounds(235, 50, 430, 60);
        pan2.setBounds(235, 120, 430, 50);
        pan3.setBounds(235, 190, 430, 50);
        pan4.setBounds(235, 260, 430, 50);
        pan5.setBounds(235, 330, 430, 50);
        pan6.setBounds(235, 400, 430, 50);
        pan7.setBounds(235, 470, 430, 50);
        pan8.setBounds(440, 530, 100, 50);

        // 向面板中添加组件
        pan1.add(label1);
        pan2.add(label2);
        pan2.add(textBno);
        pan3.add(label3);
        pan3.add(textBname);
        pan4.add(label4);
        pan4.add(textBauthor);
        pan5.add(label5);
        pan5.add(textBpress);
        pan6.add(label6);
        pan6.add(textBCategory);
        pan7.add(label7);
        pan7.add(textBnumber);
        pan8.add(button1);

        // 给面板添加边框
        pan2.setBorder(BorderFactory.createLineBorder(Color.GRAY, 2));
        pan3.setBorder(BorderFactory.createLineBorder(Color.GRAY, 2));
        pan4.setBorder(BorderFactory.createLineBorder(Color.GRAY, 2));
        pan5.setBorder(BorderFactory.createLineBorder(Color.GRAY, 2));
        pan6.setBorder(BorderFactory.createLineBorder(Color.GRAY, 2));
        pan7.setBorder(BorderFactory.createLineBorder(Color.GRAY, 2));
        //pan8.setBorder(BorderFactory.createLineBorder(Color.GRAY, 2));

        //添加面板
        pan.add(pan1);
        pan.add(pan2);
        pan.add(pan3);
        pan.add(pan4);
        pan.add(pan5);
        pan.add(pan6);
        pan.add(pan7);
        pan.add(pan8);
        frame.setContentPane(pan);

        //提交事件监听
        button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Bookop bookMessage = new Bookop();

                String bno = textBno.getText();
                String bname = textBname.getText();
                String bauthor = textBauthor.getText();
                String bpress = textBpress.getText();
                String bcategory = textBCategory.getText();
                String bnumberText = textBnumber.getText();

                //注册时，判断输入文本框的值不能为空
                if(StringUtil.isEmpty(bno)) {
                    JOptionPane.showMessageDialog(null, "书籍编号不能为空");
                    return;
                } else if(StringUtil.isEmpty(bname)){
                    JOptionPane.showMessageDialog(null, "书籍名字不能为空！");
                    return;
                } else if(StringUtil.isEmpty(bauthor)){
                    JOptionPane.showMessageDialog(null, "作者不能为空！");
                    return;
                } else if(StringUtil.isEmpty(bpress)){
                    JOptionPane.showMessageDialog(null, "出版商不能为空！");
                    return;
                }else if(StringUtil.isEmpty(bcategory)){
                    JOptionPane.showMessageDialog(null, "图书类别不能为空！");
                    return;
                }else if(StringUtil.isEmpty(bnumberText)){
                    JOptionPane.showMessageDialog(null, "书籍数量不能为空！");
                    return;
                }
                int bnumber = 0;  // 默认值
                try {
                    // 尝试将文本框中的内容转换为整数
                    bnumber = Integer.parseInt(bnumberText);
                } catch (NumberFormatException m) {
                    // 如果输入的不是有效的数字，弹出提示框
                    JOptionPane.showMessageDialog(null, "请输入有效的数字！");
                    return;
                }

                //将书籍输入的信息封装到bookMessage类里面
                bookMessage.setBno(bno);
                bookMessage.setBname(bname);
                bookMessage.setBauthor(bauthor);
                bookMessage.setBpress(bpress);
                bookMessage.setBcategory(bcategory);
                bookMessage.setBnumber(bnumber);

                CDataBase connection = new CDataBase();
                Bookop bookdao = new Bookop();

                try {
                    Connection con = connection.getConnection();

                    int current = bookdao.update(con,bookMessage);

                    //提示框，若返回值是1，添加成功
                    if(current == 1) {
                        JOptionPane.showMessageDialog(null, "修改成功！");
                        frame.dispose();
                        return;
                    } else {
                        JOptionPane.showMessageDialog(null, "修改失败,请检查书籍编号是否正确！");
                        return;
                    }
                }catch(Exception evt) {
                    evt.printStackTrace();
                }
            }
        });

        //窗口设置
        frame.setBounds(300, 100, 950, 650);
        frame.setVisible(true);
    }
}
